package com.kfgj.hkp.service.history;


import com.kfgj.hkp.common.context.CurrentContext;
import com.kfgj.hkp.common.dataaccess.PageResult;
import com.kfgj.hkp.common.exception.BusinessException;
import com.kfgj.hkp.common.service.IServiceResponse;
import com.kfgj.hkp.common.service.SpringRestService;
import com.kfgj.hkp.common.user.entity.BaseUserEntity;
import com.kfgj.hkp.common.utils.ParamUtil;
import com.kfgj.hkp.entity.history.AddressBookEntity;
import com.kfgj.hkp.entity.history.AtsigninEntity;
import com.kfgj.hkp.entity.history.TextLegalEntity;
import com.kfgj.hkp.entity.user.UserEntity;
import com.kfgj.hkp.manager.history.IAddressBookManager;
import com.kfgj.hkp.manager.history.ITextLegalManager;
import com.kfgj.hkp.manager.user.IUserManager;
import com.kfgj.hkp.service.history.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
* @Description 描述:
* @Auth xpp
* @Date 2018/11/27 10:57
* @Version 1.0
* @Param 
* @return 
**/
@RestController
@RequestMapping(path = "/textlegal")
public class TextLegalService extends SpringRestService {

    @Autowired
    private ITextLegalManager textLegalManager;
    @Autowired
    private IAddressBookManager addressBookManager;
    @Autowired
    private IUserManager userManager;


    /**
     * 创建历史记录
     * @param request
     * @return
     */
    @RequestMapping(path = "/querycootext",
            method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<IServiceResponse> create(@RequestBody TextLegalRequest request) {

        TextLegalResponse response = new TextLegalResponse();

        TextLegalEntity saEntity = new TextLegalEntity();
        Map param = new HashMap<String, Object>();
        param.put("textCode", "cootext");
        saEntity = textLegalManager.findOne(param);

        response.setResult(saEntity);
        return this.success(response);
    }


    /**
     * 申请离开显示协议
     * @param request
     * @return
     */
    @RequestMapping(path = "/queryLeaveText",
            method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<IServiceResponse> queryLeaveText(@RequestBody TextLegalRequest request) {

        TextLegalResponse response = new TextLegalResponse();

        TextLegalEntity saEntity = new TextLegalEntity();
        Map param = new HashMap<String, Object>();
        param.put("textCode", "leaveText");
        saEntity = textLegalManager.findOne(param);

        response.setResult(saEntity);
        return this.success(response);
    }


    /**
     * 新增上传通讯录
     * @param request
     * @return
     */
    @RequestMapping(path = "/addressbookAdd",
            method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<IServiceResponse> addressbookAdd(@RequestBody AddressBookRequest request) {

        AddressBookResponse response = new AddressBookResponse();

        List<AddressBookEntity> addressbookList = request.getAddressBookList();
        if(null==addressbookList){
            response.setResult("addressbookList为空!");
            return this.success(response);
        }
        if(addressbookList.size()==0){
            response.setResult("addressbookList数量为0,进行中!");
            return this.success(response);
        }

        int i = textLegalManager.addressbookInsert(addressbookList);

        response.setResult(addressbookList.size());
        return this.success(response);
    }

    /**
     * 查询上传通讯录开关
     * @param request
     * @return
     */
    @RequestMapping(path = "/queryAddressSwitch",
            method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<IServiceResponse> queryAddressSwitch(@RequestBody TextLegalRequest request) {

        TextLegalResponse response = new TextLegalResponse();

        TextLegalEntity saEntity = new TextLegalEntity();
        Map param = new HashMap<String, Object>();
        param.put("textCode", "addressSwitch");
        saEntity = textLegalManager.findOne(param);
        if(null==saEntity){
            throw new BusinessException("没有addressSwitch实体!");
        }
        String legal = saEntity.getTextLegal();
        if(legal==null){
            throw new BusinessException("getTextLegal为空!");
        }
        //1 on,off为总开关
        saEntity.setOpenFlag(false);
        if(legal.equals("on")) {
            //1.1子开关,根据用户表是否验证来开关
            BaseUserEntity userInfo = CurrentContext.getUserInfo();
            UserEntity userEntity = userManager.selectUniqueByProp(userInfo.getUserCode());
            if(null!=userEntity){
                Boolean needAuth = userEntity.getNeedAuth();
                if(null!=needAuth){
                    if(needAuth){
                        int i = addressBookManager.countByCreateTime();

                        if(i==0){
                            saEntity.setOpenFlag(true);
                        }

                    }

                }
            }

        }

        response.setResult(saEntity);
        return this.success(response);
    }

    /**
     * 新增签到
     * @param request
     * @return
     */
    @RequestMapping(path = "/atsigninAdd",
            method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<IServiceResponse> atsigninAdd(@RequestBody AtsigninRequest request) throws IllegalAccessException {

        AtsigninResponse response = new AtsigninResponse();

        AtsigninEntity atsigninEntity = request.getEntity();
        //1,校验前端必传字段
        // 根据业务需求对应字段校验
        List<String> list = new ArrayList();
        list.add("lng");
        list.add("lat");
        list.add("placeName");
        list.add("deviceName");

        ParamUtil<AtsigninEntity> paramUtil = new ParamUtil<>();
        paramUtil.check(atsigninEntity,list);

        int i = textLegalManager.atsigninAdd(atsigninEntity);

        response.setResult("签到成功!");
        return this.success(response);
    }


    /**
     * 查询我的签到--手机端
     * @param request
     * @return
     */
    @RequestMapping(path = "/atsigninQuery",
            method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<IServiceResponse> atsigninQuery(@RequestBody AtsigninRequest request) throws IllegalAccessException {

        AtsigninResponse response = new AtsigninResponse();

        AtsigninEntity atsigninEntity = request.getEntity();
        //1,校验前端必传字段
        // 根据业务需求对应字段校验
        //List<String> list = new ArrayList();
        //list.add("lng");
        //list.add("lat");
        //list.add("placeName");
        //list.add("deviceName");
        //
        //ParamUtil<AtsigninEntity> paramUtil = new ParamUtil<>();
        //paramUtil.check(atsigninEntity,list);

        Integer startInt = request.getStart();
        if(null==startInt){
            throw new BusinessException("请传入分页参数start");
        }
        Integer pageSize = request.getPageSize();
        if(null==pageSize){
            throw new BusinessException("请传入分页参数pageSize");
        }

        Date startTime = null;
        Date endTime = null;
        startTime = request.getStartTime();
        endTime = request.getEndTime();

        PageResult<AtsigninEntity> list = textLegalManager.atsigninQuery(atsigninEntity,startInt,pageSize,startTime,endTime);

        response.setResult(list);
        return this.success(response);
    }

}
